package com.mach.platform.sprider.dev;

import com.mach.platform.util.SqlHelper;
import lombok.extern.slf4j.Slf4j;

/*
    水泵状态
 */
@Slf4j
public class WaterDumpKNDDev extends GerenalStatusDev implements TransducerFaultStatusDev{


    protected Integer diNum = 1 ;
    protected Integer diNumFalut = 8;

    protected SqlHelper sqls;

    public WaterDumpKNDDev(String ip, Integer port, Integer devID,Integer diNum, String devName) {
        this( ip,  port,  devID, diNum,  devName,null);
    }

    public WaterDumpKNDDev(String ip, Integer port, Integer devID,Integer diNum, String devName,Integer diNumFault) {
        super(ip,port,devID,devName);

        this.diNum = diNum;
        if(diNumFault != null){
            this.diNumFalut = diNumFault;
        }

    }


    /*
      取变频器状态, Null则为无数据,0则为异常,1为正常
   */
    @Override
    public Integer getStatus() {
        Integer result = 3;

        IntNum startAddrNum = new IntNum(200);
        IntNum lenNUmum = new IntNum(4);

        byte[] rt = {};

        byte[] sendData = new byte[6];

        sendData[0] = this.devID.byteValue();
        sendData[1] = 2;
        sendData[2] = startAddrNum.h2();
        sendData[3] = startAddrNum.l2();
        sendData[4] = lenNUmum.h2();
        sendData[5] = lenNUmum.l2();

        byte[] sendDataHasCRC = UU.setCrc16Btye(sendData);

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendDataHasCRC);

        try {
            rt = this.wk.command(sendDataHasCRC);
            System.out.print("devId:" + this.getDevAddr() + " --> bytes>");//debug log
            UU.printData(rt);

            byte rtValue = rt[3];//第4个字节

            if((rtValue & this.diNum.byteValue()) > 0){
                result = 1;
            }else{
                result = 3;
            }
//            if(this.isFault() == 1){
//                result = 2;
//            }
        } catch (Exception e) {
//            e.printStackTrace();
            log.info("excep devId:" + this.devName + " " + e.getMessage());
        }



        return result;
    }


    public static void main(String[] args){
        WaterDumpKNDDev td = new WaterDumpKNDDev("192.168.3.91",8003,4,1,"14#变频器（PA2、PO2工艺24H）");
        td.getStatus();
    }

    @Override
    public Integer isFault() {
        return this.isFault(this.diNumFalut);
    }

    @Override
    public Integer isFault(Integer biNum) {
        Integer result = 0;

        IntNum startAddrNum = new IntNum(200);
        IntNum lenNUmum = new IntNum(5);

        byte[] rt = {};

        byte[] sendData = new byte[6];

        sendData[0] = this.devID.byteValue();
        sendData[1] = 2;
        sendData[2] = startAddrNum.h2();
        sendData[3] = startAddrNum.l2();
        sendData[4] = lenNUmum.h2();
        sendData[5] = lenNUmum.l2();

        byte[] sendDataHasCRC = UU.setCrc16Btye(sendData);

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendDataHasCRC);

        try {
            rt = this.wk.command(sendDataHasCRC);
            System.out.print("devId:" + this.getDevAddr() + " --> bytes>");//debug log
            UU.printData(rt);

            byte rtValue = rt[3];//第4个字节

            if((rtValue & biNum.byteValue()) < 1){
                result = 1;
            }else{
                result = 0;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            log.info("excep devId:" + this.devName + " " + e.getMessage());
        }



        return result;
    }
}
